package top.itaso.app.ui.floating;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import top.itaso.app.R;
import top.itaso.lib.alog.ALog;
import top.itaso.lib.common.ToastUtils;

/**
 * @Description: 悬浮窗口
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2022/02/08
 */
public class FloatingWindowView extends ConstraintLayout {
    private static final String TAG = "FloatingWindowView";
    private WindowManager.LayoutParams mFloatingWindowLayoutParams;

    private TextView mTvContent;

    public FloatingWindowView(Context context) {
        this(context, null);
    }

    public FloatingWindowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatingWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        LayoutInflater.from(context).inflate(R.layout.layout_floating_window, null);
        View rootRoot = inflate(getContext(), R.layout.layout_floating_window, this);
        initView(rootRoot);
        FloatingWindowDragListener floatingWindowDropListener = new FloatingWindowDragListener((WindowManager) context.getSystemService(Context.WINDOW_SERVICE), getFloatingWindowLayoutParams(), this, true);
        this.setOnTouchListener(floatingWindowDropListener);
    }

    private void initView(View rootRoot) {
        mTvContent = rootRoot.findViewById(R.id.tv_content);
        rootRoot.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ALog.dTag(TAG, "悬浮窗被点击了");
                ToastUtils.getInstance().showShortToast(getContext(), "悬浮窗被点击了");
            }
        });
    }

    public void refreshUI(String planeContent) {
        mTvContent.setText(planeContent);
    }

    public WindowManager.LayoutParams getFloatingWindowLayoutParams() {
        if (mFloatingWindowLayoutParams == null) {
            mFloatingWindowLayoutParams = new WindowManager.LayoutParams();
            mFloatingWindowLayoutParams.windowAnimations = R.style.Style_FloatingViewAnimationIn;

        }
        mFloatingWindowLayoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        mFloatingWindowLayoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        mFloatingWindowLayoutParams.gravity = Gravity.START | Gravity.TOP;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mFloatingWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            mFloatingWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        mFloatingWindowLayoutParams.format = PixelFormat.RGBA_8888;
        mFloatingWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        return mFloatingWindowLayoutParams;
    }
}
